; Exercicio 1.16
(require (planet schematics/schemeunit:3))

(define (fast-expt-it b n)
  (fast-expt-it-aux b n 1))

(define (fast-expt-it-aux b n a)
  (cond ((= n 0)   a)
        ((even? n) (fast-expt-it-aux (square b) (/ n 2) a))
        (else      (fast-expt-it-aux  b         (- n 1) (* b a)))))

(define (square n) (* n n))

(check-equal? (fast-expt-it 2 0) 1)
(check-equal? (fast-expt-it 2 1) 2)
(check-equal? (fast-expt-it 2 2) 4)
(check-equal? (fast-expt-it 2 3) 8)
(check-equal? (fast-expt-it 2 12) 4096)
(check-equal? (fast-expt-it 3 5) 243)
(check-equal? (fast-expt-it 3 7) 2187)
